home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Tools&Utilities / Digester 2.3 / sources / post-send.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-14  |  4.2 KB  |  179 lines  |  [TEXT/ttxt]

  1. /* 
  2.     file:     post-send
  3.     by:        A.C. van der Ham
  4.     e-mail:    a.c.vanderham@et.tudelft.nl
  5.     
  6.     purpose:    to be used together with digester to enable easy reply to messages
  7.     warning:    only works with UNIX servers for now
  8.     where:    compile it and put it in your html scripts directory
  9.     notice:    don't forget to set the correct path for the info-mac files
  10. */
  11.  
  12. #define    PATH        "/home/vdham/Mosaic/info-mac/"
  13.  
  14.  
  15. #include    <stdio.h>
  16. #include    <stdlib.h>
  17. #include    <strings.h>
  18.  
  19. #define    MAXLINE    512
  20. #define    OK            0
  21. #define    ERROR        1
  22. #define    ITEMTEXT    "<a name=item%d"
  23. #define    INFOMAC    "info-mac@sumex-aim.stanford.edu"
  24. #define    EOM        "<a name=item"
  25.  
  26. int IsEmailChar( char c )
  27. {
  28.     if( (c>='a') && (c<='z') ) return(1);
  29.     if( (c>='A') && (c<='Z') ) return(1);
  30.     if( (c>='0') && (c<='9') ) return(1);
  31.     if(c=='.') return(1);
  32.     if(c=='_') return(1);
  33.     if(c=='-') return(1);
  34.     if(c=='%') return(1);
  35.     return(0);
  36. }
  37.  
  38. int findmessage( int item, char *file )
  39. {
  40.     FILE    *inP;
  41.     char    line[256], searchString[100], address[80], *cptr, *begin;
  42.     
  43.     strcpy( line, PATH );
  44.     strcat( line, file );
  45.  
  46.     searchString[0]=0;
  47.     
  48.     
  49.     sprintf( searchString, ITEMTEXT, item );
  50.  
  51.     strcpy( address, INFOMAC );
  52.  
  53.     if( (inP = fopen( line, "r" )) != NULL )
  54.     {    
  55.         do {
  56.                 fgets( line, 256, inP );
  57.         } while( (!feof(inP)) &&
  58.                     (strstr(line, searchString)==NULL) );
  59.         
  60.         if( feof(inP) )
  61.             return(ERROR);
  62.         
  63.         fputs( "<pre>", stdout );
  64.             
  65.         do {
  66.                 fgets( line, 256, inP );
  67.                 
  68.                 /* print lines that do not contain code */
  69.                 if( (strstr(line,"<")==NULL) && (strstr(line,">")==NULL) )
  70.                     fputs( line, stdout );
  71.  
  72.                 
  73.                 /* check for the From string */
  74.                 if( (cptr=strstr( line, "From: " ))!=NULL )
  75.                 {
  76.                     cptr += 6;
  77.                     strcpy(address, cptr);
  78.                     
  79.                     /* terminate the address string */
  80.                     
  81.                     if( (cptr=strstr(address,"\n")) != NULL)
  82.                         *cptr=0;
  83.  
  84.                     /* look for delimiters < and >
  85.                     if( (cptr=strstr(address,"<")) != NULL)
  86.                     {
  87.                         strcpy(address, cptr+4);
  88.                         if( (cptr=strstr(address,">")) != NULL)
  89.                             *cptr=0;
  90.                     }
  91.                     */
  92.                     
  93.                     /* find any spaces an terminate from there
  94.                     if( (cptr=strstr(address," ")) != NULL)
  95.                         *cptr=0;
  96.                     */
  97.                     
  98.                     strcpy(searchString, address);
  99.                     
  100.                     /* more intelligent e-mail parser */
  101.                     if( (cptr=strstr(searchString,"@"))!=NULL)
  102.                     {
  103.                         /* find beginning */
  104.                         begin=cptr; begin--;
  105.                         while(IsEmailChar(*begin) && begin>=searchString) begin--;
  106.                         
  107.                         /* find ending */
  108.                         begin++; cptr++;
  109.                         while(IsEmailChar(*cptr) && *cptr!=0 ) cptr++;
  110.                         
  111.                         /* terminate address and copy */
  112.                         *cptr=0;
  113.                         strcpy(address, begin);
  114.                     }
  115.                     else
  116.                         strcpy(address, INFOMAC);
  117.                 }
  118.         } while( (!feof(inP)) &&
  119.                     (strstr(line, EOM)==NULL) );
  120.  
  121.         fputs( "</pre>", stdout );
  122.                             
  123.         fprintf( stdout, "Reply to: <a href=mailto:%s>%s</a><p>\n", address, address );
  124.         fprintf( stdout, "Reply to: <a href=mailto:%s>%s</a><p>\n", INFOMAC, INFOMAC );
  125.  
  126.         fclose(inP);
  127.     }
  128.     else
  129.     {
  130.         return(ERROR);
  131.     }
  132.     return(OK);
  133. }
  134.  
  135.  
  136. int main( int argc, char *argv[] )
  137. {
  138.     char    *cptr;
  139.     char    file[80];
  140.     int    item=0;
  141.     
  142.     argc=argc;
  143.     
  144.     fprintf(stdout, "Content-type: text/html\n\n");
  145.  
  146.     if( argv[1]!=NULL)
  147.     {
  148.         if((cptr=strstr(argv[1],","))!=NULL)
  149.         {
  150.             *cptr=0;
  151.             item=atoi(argv[1]);
  152.             strcpy(file, cptr+1);
  153.             if( findmessage( item, file ) == ERROR)
  154.             {            
  155.                 fprintf(stdout, "<title>can't find message</title>\n");
  156.                 fprintf(stdout, "Sorry, something went wrong in message selection.<p>\n");
  157.                 fprintf(stdout, "Please notify: <a href=mailto:a.c.vanderham@et.tudelft.nl>André van der Ham</a>.<p>\n");
  158.                 fprintf(stdout, "ERROR: can't find message?!\n");
  159.             }
  160.         }
  161.         else
  162.         {
  163.             fprintf(stdout, "<title>no delimiter</title>\n");
  164.             fprintf(stdout, "Sorry, something went wrong in message selection.<p>\n");
  165.             fprintf(stdout, "Please notify: <a href=mailto:a.c.vanderham@et.tudelft.nl>André van der Ham</a>.<p>\n");
  166.             fprintf(stdout, "ERROR: File name delimiter not found?!\n");
  167.         }
  168.     }
  169.     else
  170.     {
  171.         fprintf(stdout, "<title>no arguments</title>\n");
  172.         fprintf(stdout, "Sorry, something went wrong in message selection.<p>\n");
  173.         fprintf(stdout, "Please notify: <a href=mailto:a.c.vanderham@et.tudelft.nl>André van der Ham</a>.<p>\n");
  174.         fprintf(stdout, "ERROR: Can't find any arguments to query?!\n");
  175.     }
  176.     
  177.     return(0);
  178. }
  179.